package com.pxst.demo.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.pxst.enums.AmqEnum;
import com.pxst.resp.ApiResp;
import com.pxst.utils.AmqUtils;
import com.pxst.utils.ExcelUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.codeleep.jsondiff.common.model.Defects;
import me.codeleep.jsondiff.common.model.JsonCompareResult;
import me.codeleep.jsondiff.common.model.JsonComparedOption;
import me.codeleep.jsondiff.common.model.TravelPath;
import me.codeleep.jsondiff.core.DefaultJsonDifference;

/** 
* @author yangxy
* @version 创建时间：2023年7月28日 上午11:43:22 
*/
@Slf4j
@RestController
@Api(tags = "demo")
public class TestController {
	@Autowired
	private TestService testService;
	
	@GetMapping("/test")
	@ApiOperation(value = "分布式事务示例")
//	@LcnTransaction(propagation = DTXPropagation.SUPPORTS)//分布式事务参与方
	public ApiResp<String> test(String name){
		return testService.test(name);
	}
	
	@ApiOperation(value = "excel导出示例")
	@GetMapping("/test/excel/export")
	public void test1(HttpServletResponse response) throws IOException {
		List<ExcelModel1> list = Lists.newArrayList(new ExcelModel1("1","2"),new ExcelModel1("3","4"));
		List<ExcelModel> liar = Lists.newArrayList(new ExcelModel("test1",0, list),new ExcelModel("test2",1, list));

        ExcelUtils.exportExcel(liar, "用户信息统计表Title", "用户表Sheet", ExcelModel.class, "用户信息统计File", response);
	}

	@ApiOperation(value = "调用插件示例")
	@GetMapping("/test/jar/test")
	public void test()  {
		testService.test();
	}

	@Autowired
	private AmqUtils amqUtils;
	
	@ApiOperation(value = "延迟消息队列示例")
	@GetMapping("/test/mq/delayed")
	public void delayedMq(String msg) {
		log.info("发送延迟队列:{}",msg);
		amqUtils.sendDelayedMsg(AmqEnum.APP_SALVAGE_ACCOUNT.exchange, AmqEnum.APP_SALVAGE_ACCOUNT.routingKey, msg, 5);
	}
	
	public static void main(String[] args) {
		Map<String, String> map = Maps.newHashMap();
		map.put("test", "1");
		map.put("test1", "2");
		map.put("test3", "4");
		Map<String, String> map1 = Maps.newHashMap();
		map1.put("test", "1");
		map1.put("test1", "2");
		map1.put("test3", "5");
		map1.put("test4", "7");
		map1.put("test5", "8");
		String jsonString = JSON.toJSONString(map);
		JSONObject parseObject = JSON.parseObject(jsonString);
		String jsonString2 = JSON.toJSONString(map1);
		JSONObject parseObject2 = JSON.parseObject(jsonString2);
		JsonComparedOption jsonComparedOption = new JsonComparedOption().setIgnoreOrder(true);
		JsonCompareResult jsonCompareResult = new DefaultJsonDifference().option(jsonComparedOption)
				.detectDiff(parseObject, parseObject2);
		List<Defects> defectsList = jsonCompareResult.getDefectsList();
		for(Defects defects: defectsList) {
			TravelPath travelPath = defects.getTravelPath();
			String expectTravelPath = travelPath.getExpectTravelPath();
			String replace = expectTravelPath.replace("root.", "");
			if("null".equals(replace)) {
				String replace2 = travelPath.getActualTravelPath().replace("root.", "");
				Object actual = defects.getActual();
				System.out.println(String.format("%s:%s->%s", replace2,null,actual));
			}else {
				String replace2 = travelPath.getActualTravelPath().replace("root.", "");
				Object actual = defects.getActual();
				Object expect = defects.getExpect();
				System.out.println(String.format("%s:%s->%s", replace2,expect,actual));
			}
		}
		
	}
}
